package com.lc.cehui.ui.activity;

import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.hjq.bar.TitleBar;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.cehui.R;
import com.lc.cehui.app.AppActivity;
import com.lc.cehui.http.request.CompanyOtherShowApi;
import com.lc.cehui.http.response.CompanyOtherShowBean;
import com.lcodecore.tkrefreshlayout.utils.LogUtil;

import butterknife.BindView;
import butterknife.ButterKnife;

public class WebVIewActivity extends AppActivity {
    @BindView(R.id.titlebar)
    TitleBar titlebar;
    @BindView(R.id.webView)
    WebView webView;
    private String newsTextSrart = "<html>" +
            "<head>" +
            "<meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0'>" +
            "</head>" +
            "<body>";

    private String newsTextEnd = "<style type=\"text/css\"> img {" +
            "width: 100% !important;" +
            "height:auto !important;" +
            "}" +
            "</style>" +
            "</body></html>";
    @Override
    protected int getLayoutId() {
        return R.layout.activity_webview;
    }

    @Override
    protected void initView() {
        company_other_show();
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setUseWideViewPort(true);//设置webview推荐使用的窗口
        webSettings.setLoadWithOverviewMode(true);//设置webview加载的页面的模式
        webSettings.setDisplayZoomControls(false);//隐藏webview缩放按钮
        webSettings.setJavaScriptEnabled(true); // 设置支持javascript脚本
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {

                LogUtil.i("webview加载结束");
            }
        });
    }

    private void company_other_show() {
        EasyHttp.post(this)
                .api(new CompanyOtherShowApi().setType_id(getIntent().getStringExtra("type_id")).setInfo_id(getIntent().getStringExtra("info_id")))
                .request(new HttpCallback<CompanyOtherShowBean>(this) {

                    @Override
                    public void onSucceed(CompanyOtherShowBean data) {

                        if (data.getCode().equals("200")) {
                                   titlebar.setTitle(data.getData().getTitle());
                            webView.loadDataWithBaseURL(null, newsTextSrart + data.getData().getContent() + newsTextEnd
                                    , "text/html", "UTF-8", null);

                        }

                    }
                });

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
